<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="1100px">
    <ContentWrap>
      <el-form :model="queryParams" ref="queryFormRef" label-width="auto" :inline="true">
        <el-form-item label="老人姓名" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入老人姓名"
            :clearable="true"
            @keyup.enter="getPage"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请输入状态" :clearable="true">
            <el-option
              v-for="dict in getIntDictOptions(DICT_TYPE.FEE_RECHARGE_DETAIL_STATUS)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          /></el-select>
        </el-form-item>
        <el-form-item label="缴费时间" prop="payTime">
          <el-date-picker
            v-model="queryParams.payTime"
            type="daterange"
            value-format="YYYY-MM-DD HH:mm:ss"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleClickSearch">
            <Icon icon="ep:search" />
            搜索
          </el-button>
          <el-button @click="handleClickReset">
            <Icon icon="ep:refresh" />
            重置
          </el-button>
        </el-form-item>
      </el-form>
      <Table
        :columns="columns"
        :page-param="queryParams"
        :adaptive="true"
        :page-data="queryData"
        save-key="FeeRechargeDetail"
        height="500"
        @page-change="getPage"
      >
        <template #menu="{ row }">
          <context-menu-item
            label="确认缴费"
            @click="handleMenuConfirm(row)"
            v-hasPermi="['fee:recharge:confirm']"
          />
        </template>
      </Table>
    </ContentWrap>
  </Dialog>
</template>
<script setup name="FeeRechargeDetail" lang="ts">
import * as FeeRechargeApi from '@/api/fee/feeRecharge'
import * as FeeCategoryApi from '@/api/fee/feeCategory'
import { DICT_TYPE, getDictObj, getIntDictOptions } from '@/utils/dict'
const feeCategoryList = ref()
const message = useMessage()
const dialogTitle = ref('')
const dialogVisible = ref(false)
const queryFormRef = ref()
const queryParams = ref({
  name: undefined,
  status: undefined,
  payTime: undefined
})
const queryData = ref()
const columns = [
  {
    label: '姓名',
    prop: 'elder.name'
  },
  {
    label: '身份证号',
    prop: 'elder.identityCard'
  },
  {
    label: '手机号',
    prop: 'elder.mobile'
  },
  {
    label: '金额',
    prop: 'rechargeFee'
  },
  {
    label: '状态',
    prop: 'status',
    formatter: ({ status }) => getDictObj(DICT_TYPE.FEE_RECHARGE_DETAIL_STATUS, status).label
  },
  {
    label: '缴费方式',
    prop: 'payType',
    formatter: ({ payType }) => getDictObj(DICT_TYPE.FEE_PAY_TYPE, payType).label
  },
  {
    label: '费用类别',
    prop: 'feeCategoryId',
    formatter: ({ feeCategoryId }) =>
      feeCategoryList.value.find((obj) => obj.id === feeCategoryId).categoryName
  },
  {
    label: '缴费时间',
    prop: 'payTime'
  },
  {
    label: '费用说明',
    prop: 'description'
  },
  {
    label: '创建时间',
    prop: 'createTime'
  }
]
/** 打开弹窗 */
const open = async () => {
  feeCategoryList.value = await FeeCategoryApi.listQuery()
  getPage()
  dialogVisible.value = true
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 搜索按钮操作 */
const handleClickSearch = () => {
  getPage()
}

/** 重置按钮操作 */
const handleClickReset = () => {
  queryFormRef.value?.resetFields()
  handleClickSearch()
}

/** 处理响应事件  */
const getPage = async () => {
  queryData.value = await FeeRechargeApi.pageQueryDetail(queryParams.value)
}

/** 处理右击事件 确认缴费 */
const handleMenuConfirm = async (row) => {
  await FeeRechargeApi.confirm(row.id)
  message.success('确认缴费成功')
}
</script>
